$(function () {
    setTimeout(function () {
       $('[data-toggle="tooltip"]').tooltip({html: true})
    }, 200);

    //操作引导
    SessionUtil.showOneTimeTip("process_form_guide", function(){
        introJs().start().onbeforeexit(function () {
            return true;
        });
    });
    
});

//挑选活动
function chooseAct(){
    dialogOpenPageConfirm('选择活动', 'actChooseList.html', 700, 400, function () {
        //confirm
        var iframe = $(document).find(".jconfirm-content iframe")[0].contentWindow;
        var array = iframe.retAllSelect();
        if(array && array.length > 0){
            var item = null;
            for(var i=0; i<array.length; i++){
                item = array[i];
                appendActLi(item.id, item.name);
            }
        }
        bindActItemEdit();
        return true; //false则窗口不关闭
    }, function () {
        //init
    });
}

//追加 活动元素
function appendActLi(id, name){
    //禁止重复
    log(actListUL);
    if($("#actListUL").find("li label[data-id="+id+"]").length == 1){
        dialogTipWarn("已自动跳过 重复的活动");
        return;
    }
    var el = document.createElement('li');
    el.innerHTML = '<label data-id="'+id+'">['+id+'] '+name + '<i class="js-remove">✖</i></label>';
    actList.el.appendChild(el);
}

//加载活动元素
function loadActLi(idStr, callback){
    if(CommUtil.isNotEmpty(idStr)){
        HttpUtil.ajaxAsyncJsonPost("/bflow/act/findByIds", {ids: idStr}, function(array){
            console.log("array", array);
            var id, item, ids = idStr.split(",");
            log(ids);
            for(var i=0; i<ids.length; i++){ //防止 返回列表 可能已去重复
                id = ids[i];
                log(id);
                for(var j=0; j<array.length; j++){
                    item = array[j];
                    log(item);
                    if(id == item.id)
                        appendActLi(item.id, item.name);
                }
            }
            return callback(ids);
        });
    }
}

//构建 路由条件
function buildRouteItem(actId, actSort, isAppend, item){
    var groupId = CommUtil.getRandom();
    if(item == undefined){
        item = new Object(); //防止空异常
    }
    var sb = new StringBuffer();
    sb.append('<div class="form-group" id="'+groupId+'">');
    sb.append('<input type="hidden" name="actId" value="'+actId+'" class="form-control">');
    sb.append('<input type="hidden" name="actSort" value="'+actSort+'" class="form-control">');
    sb.append('<div class="col-sm-2"><input type="text" name="condDesc" maxlength="12" class="form-control" value="'+CommUtil.toText(item.condDesc)+'" placeholder="条件说明" required></div>');
    sb.append('<div class="col-sm-2"><input type="text" name="condKey" maxlength="32" class="form-control" value="'+CommUtil.toText(item.condKey)+'" placeholder="条件KEY标识"></div>');
    sb.append('<div class="col-sm-2"><input type="number" name="nextActId" maxlength="4" class="form-control" value="'+CommUtil.toText(item.nextActId)+'" placeholder="下个活动ID"></div>');
    sb.append('<div class="col-sm-3"><input type="text" name="callbackBeforeAct" maxlength="64" class="form-control" value="'+CommUtil.toText(item.callbackBeforeAct)+'" placeholder="活动前回调"></div>');
    sb.append('<div class="col-sm-3"><input type="text" name="callbackAfterAct" maxlength="64" class="form-control" value="'+CommUtil.toText(item.callbackAfterAct)+'" placeholder="活动后回调"></div>');
    if(isAppend && isAppend==true){
        sb.append('<a href="javascript:removeRouteItem(\''+groupId+'\')" class="pull-right mgr-12">移除</a>');
    }
    sb.append('</div>');
    return sb.toString();
}

function removeRouteItem(groupId){
    return $("#"+groupId).remove();
}

function addRouteItem(id, actSort){
    var str = buildRouteItem(id, actSort, true);
    $("#addBtn").before(str);
}

//设置 隐藏表单 actSet
function setHiddenInputActSet(){
    var ul = $("#actListUL");
    if(ul.length == 0){
        dialogTipError("活动集合不能为空");
        return false;
    }
    var actSet = new Array();
    ul.find("li").each(function () {
        actSet.push($(this).find("label").data("id"));
    });
    if(actSet.length == 0){
        dialogTipError("活动集合不能为空");
        return false;
    }
    $("#actSet").val(actSet.join(","));
    return true;
}